package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.SubscribeMapper;
import com.example.mapper.UserMapper;
import com.example.model.Subscribe;
import com.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Author by admin, Email xx@xx.com, Date on 2020/5/28.
 * PS: Not easy to write code, please indicate.
 */
@Service
public class SubscribeService extends ServiceImpl<SubscribeMapper, Subscribe> {

    @Autowired
    SubscribeMapper subscribeMapper;
    @Autowired
    UserMapper userMapper;


    public List<User> getLikeListById(Integer userId) {
        List<Subscribe> subscribeList = subscribeMapper.selectList(new QueryWrapper<Subscribe>().eq("user_id", userId));
        List<User> userList = new ArrayList<>();
        if (!subscribeList.isEmpty()){
            subscribeList.forEach(i->{
                User author = userMapper.selectById(i.getAuthorId());
                userList.add(author);
            });
        }
        return userList;
    }

    public List<User> getSubscribeListById(int authorId) {
        List<Subscribe> subscribeList = subscribeMapper.selectList(new QueryWrapper<Subscribe>().eq("author_id", authorId));
        List<User> subscribeUserList= new ArrayList<>();
        if (!subscribeList.isEmpty()){
            subscribeList.forEach(i->{
                User user = userMapper.selectById(i.getUserId());
                subscribeUserList.add(user);
            });
        }
        return subscribeUserList;
    }

    public boolean deluser(Integer userId) {
        int user_id = subscribeMapper.delete(new QueryWrapper<Subscribe>().eq("user_id", userId));
        return user_id>0;
    }
}
